<template>
  <div>
    <BasicModal v-bind="$attrs" @register="register" :title="modalTitle" width="1024px" @ok="handleOk" destroyOnClose
      @visible-change="visibleChange">
      <BasicTable :columns="columns" v-bind="config" :useSearchForm="true" :formConfig="formConfig"
        :api="planOrderResultPageList" :searchInfo="searchInfo" :rowSelection="rowSelection"
        :indexColumnProps="indexColumnProps"></BasicTable>
    </BasicModal>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, unref, reactive } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { planOrderResultPageList } from './base.api';
import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
import { useSelectBiz } from '/@/components/Form/src/jeecg/hooks/useSelectBiz';
import { selectProps } from '/@/components/Form/src/jeecg/props/props';
import { useAttrs } from '/@/hooks/core/useAttrs';
import { queryTreeList, getUserList } from '/@/api/common/api';

export default defineComponent({
  name: 'MpsSelectModel',
  components: {
    //此处需要异步加载BasicTable
    BasicModal,
    BasicTable: createAsyncComponent(() => import('/@/components/Table/src/BasicTable.vue'), {
      loading: true,
    }),
  },
  props: {
    ...selectProps,
    //选择框标题
    modalTitle: {
      type: String,
      default: 'MRP运算结果选择',
    },
    multiple: {
      type: Boolean,
      default: false,
    },
    supplyType: {
      type: Number,
      default: 0,
    },
    demandType: {
      type: Number,
      default: 1,
    },
    isNotPlaned: {
      type: Number,
      default: 1
    }
  },

  emits: ['register', 'getSelectResult'],
  setup(props, { emit }) {
    //注册弹框
    const [register, { closeModal }] = useModalInner();
    const attrs = useAttrs();
    //表格配置
    const config = {
      canResize: false,
      bordered: true,
      size: 'small',
      rowKey: unref(props).rowKey,
      labelKey: 'planName',
      isRadioSelection: !(props.multiple == true) ?? true,

      beforeFetch(params) {
        params.supplyType = 0;
        params.demandType = 1;
        params.isNotPlaned = 1;
      },
    };
    const getBindValue = Object.assign({}, unref(props), unref(attrs), config);
    const [{ rowSelection, indexColumnProps, visibleChange, getSelectResult }] = useSelectBiz(planOrderResultPageList, getBindValue);
    rowSelection.columnWidth = 50;
    indexColumnProps.width = 50;
    const searchInfo = ref(props.params);
    const awaitList = reactive({
      data: [],
    });
    getList();
    let initTreeList = ref([]);
    const treeList = ref([]);
    async function tableData() {
      initTreeList.value = await queryTreeList();
      treeList.value = flatten(initTreeList.value);
    }

    function flatten(arr) {
      let res = [];
      for (let i = 0; i < arr.length; i++) {
        const child = arr[i].children;
        if (child?.length > 0) {
          res = res.concat(flatten(child));
        }
      }
      return res;
    }
    async function getList() {
      awaitList.data = (
        await getUserList({
          pageNo: 1,
          pageSize: 9999,
        })
      ).records;
    }
    tableData();
    //查询form
    const formConfig = {
      labelWidth: 100,
      baseColProps: {
        xs: 8,
        sm: 8,
        md: 8,
      },
      schemas: [
        {
          label: '物料编码',
          field: 'materialCode',
          component: 'JInput',
        },
        {
          label: '销售订单号',
          field: 'OrderBillCode',
          component: 'JInput',
        },
        {
          label: '订单行号',
          field: 'orderRowNumber',
          component: 'JInput',
        },
        {
          label: '计划单号',
          field: 'srcBillCode',
          component: 'JInput',
        },
        {
          label: '计划日期',
          field: 'demandDate',
          component: 'DatePicker',
          componentProps: {
            showTime: true,
            valueFormat: 'YYYY-MM-DD',
          },
        },
      ],
    };
    //定义表格列
    const columns = [
      {
        title: '订单号',
        dataIndex: 'orderBillCode',
        width: 160,
      },
      {
        title: '订单行号',
        dataIndex: 'orderRowNumber',
        width: 80,
      },
      {
        title: '计划编号',
        dataIndex: 'srcBillCode',
        width: 150,
      },
      {
        title: '计划行号',
        dataIndex: 'rowNum',
        width: 80,
      },
      {
        title: '物料编码',
        dataIndex: 'materialCode',
        width: 150,
      },
      {
        title: '物料名称',
        dataIndex: 'materialName',
        width: 100,
      },
      {
        title: '图号',
        dataIndex: 'drawingNo',
        width: 100,
      },
      {
        title: '需求数量',
        dataIndex: 'orderQuantity',
        width: 120,
      },
      {
        title: '计划日期',
        dataIndex: 'demandDate',
        width: 120,
      },
      {
        title: '需求类型',
        dataIndex: 'demandType',
        customRender: ({ text }) => (text == 0 ? '独立' : text == 1 ? '相关' : ''),
        width: 120,
      },
    ];

    /**
     * 确定选择
     */
    function handleOk() {
      getSelectResult((options, values) => {
        //回传选项和已选择的值
        emit('getSelectResult', options, values);
        //关闭弹窗
        closeModal();
      });
    }

    return {
      config,
      handleOk,
      searchInfo,
      register,
      indexColumnProps,
      visibleChange,
      planOrderResultPageList,
      formConfig,
      getBindValue,
      columns,
      rowSelection,
    };
  },
});
</script>
